package com.jiecao.news.jiecaonews.dao;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.jiecao.news.jiecaonews.pojo.NewsListItem;
import com.jiecao.news.jiecaonews.util.constants.Constants;
import com.jiecao.news.jiecaonews.util.dao.SQLiteHelper;

public class NewsDao {
	private static final String TAG = "NewsDao"; // Log Tag
	private Context mcontext = null; // context对象，activity对象带入
	private SimpleDateFormat formatter = new SimpleDateFormat(
			"yyyy-MM-dd HH:mm:ss");

	/**
	 * 构造函数
	 */
	public NewsDao(Context context) {
		mcontext = context;
	}


	public ArrayList<NewsListItem> getNewsList() {
		SQLiteHelper dbHelper = new SQLiteHelper(mcontext);
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		// 从缓存获取，如果最近没哟刷新，就并触发下拉刷新事件
		Cursor cursor = db.query(SQLiteHelper.NEWS_LISTITEM_TB_NAME,
				new String[] { "id", "icon", "intro", "publish_time", "title", "is_readed", "app_create_time",
						"share_num" }, null, null, null, null, null);

		cursor.moveToFirst();
		ArrayList<NewsListItem> mItems = new ArrayList<NewsListItem>();
		while (!cursor.isAfterLast()) {
			NewsListItem listItem = new NewsListItem();
			String date = formatter.format(new Date());
			listItem.setAppCreateTime(date);
			listItem.setId(cursor.getLong(0));
			listItem.setIcon(cursor.getString(1));
			listItem.setIntro(cursor.getString(2));
			listItem.setPublishTime(cursor.getString(3));
			listItem.setTitle(cursor.getString(4));
			listItem.setIsReaded(cursor.getInt(5));
			listItem.setShareNum(cursor.getInt(7));
			mItems.add(listItem);
			cursor.moveToNext();
		}
		cursor.close();
		db.close();
		dbHelper.close();
		return mItems;
	}

	public void storeItems(
			ArrayList<NewsListItem> mItems) {
		SQLiteHelper dbHelper = new SQLiteHelper(mcontext);
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		db.beginTransaction();
		db.delete(SQLiteHelper.NEWS_LISTITEM_TB_NAME, null, null);
		int count = mItems.size() > Constants.LIST_PAGE_SIZE ? Constants.LIST_PAGE_SIZE
				: mItems.size();
		for (int i = 0; i < count; i++) {
			ContentValues values = new ContentValues();
			values.put("id", mItems.get(i).getId());
			values.put("icon", mItems.get(i).getIcon());
			values.put("intro", mItems.get(i).getIntro());
			values.put("publish_time", mItems.get(i).getPublishTime());
			values.put("title", mItems.get(i).getTitle());
			values.put("is_readed", mItems.get(i).getIsReaded());
			values.put("app_create_time", mItems.get(i).getAppCreateTime());
			values.put("share_num", mItems.get(i).getShareNum());
			db.insert(SQLiteHelper.NEWS_LISTITEM_TB_NAME, null, values);
		}
		
		db.setTransactionSuccessful();
		db.endTransaction();
		
		db.close();
		dbHelper.close();
	}

	
	public void saveArticle(Long mId,String mNewsInfo,String mAppCreateTime){
		SQLiteHelper dbHelper = new SQLiteHelper(mcontext);
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		db.beginTransaction();
		db.delete(SQLiteHelper.NEWS_ARTICLE_TB_NAME, "id=?",new String[]{mId.toString()} );
		ContentValues values = new ContentValues();
		
		values.put("id", mId);
		values.put("art_info", mNewsInfo);
		values.put("app_create_time", mAppCreateTime);
		db.insert(SQLiteHelper.NEWS_ARTICLE_TB_NAME, null, values);
		db.setTransactionSuccessful();
		db.endTransaction();
		
		db.close();
		dbHelper.close();
	}
	public String getNewsInfoById(Long id){
		SQLiteHelper dbHelper = new SQLiteHelper(mcontext);
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		Cursor cursor = db.query(SQLiteHelper.NEWS_ARTICLE_TB_NAME,
				new String[] { "id","art_info","app_create_time" }, "id=?",
				new String[] { id.toString() }, null, null, null);
		String newsInfo = null;
		if (cursor.moveToNext()) {
			newsInfo = cursor.getString(1);
		}
		cursor.close();
		db.close();
		dbHelper.close();
		return newsInfo;
	}
	public void flush(){
		SQLiteHelper dbHelper = new SQLiteHelper(mcontext);
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		db.beginTransaction();
		db.delete(SQLiteHelper.NEWS_ARTICLE_TB_NAME, null, null);
		db.delete(SQLiteHelper.NEWS_LISTITEM_TB_NAME, null, null);
        db.setTransactionSuccessful();
		db.endTransaction();
        
		db.close();
		dbHelper.close();
	}
	

}
